package com.cyz.web.app.leetcode.twoSum;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Client {


    /**
     * 给定一个整数数组 nums 和一个整数目标值 target，
     * 请你在该数组中找出 和为目标值 target  的那 两个 整数，
     * 并返回它们的数组下标。
     * @param sums
     * @param target
     * @return
     */
    public int[] twoSum_01(int[] sums, int target){
        int len = sums.length;
        for (int i=0;i<len-1;i++){
            for (int j=i+1;j<len;j++){
                if ((sums[i] + sums[j])==target){
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }


    public int[] twoSum_02(int[] sums, int target){
        Map<Integer, Integer>   hashTable = new HashMap<>();
        for(int i=0;i<sums.length;i++){
            if (hashTable.containsKey(target - sums[i])){
                return new int[]{ hashTable.get(target-sums[i]), i};
            }
            hashTable.put(sums[i], i);
        }
        return null;
    }




    public static void main(String[] args) {
        Client c = new Client();
        int[] sums = {1,2,3,4,5,6,7,8,9,11};
        int target = 15;
        int[] ints = c.twoSum_02(sums, target);
        System.out.println(Arrays.toString(ints));
    }






}


